import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: JSON.parse(localStorage.getItem("user") || "{}"),
    token: localStorage.getItem("token") || "",
    walletInfo: null,
    transactions: [],
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
      localStorage.setItem("user", JSON.stringify(user));
    },
    setToken(state, token) {
      state.token = token;
      localStorage.setItem("token", token);
    },
    clearAuth(state) {
      state.user = {};
      state.token = "";
      localStorage.removeItem("user");
      localStorage.removeItem("token");
    },
    setWalletInfo(state, walletInfo) {
      state.walletInfo = walletInfo;
    },
    setTransactions(state, transactions) {
      state.transactions = transactions;
    },
  },
  actions: {
    async login({ commit }, credentials) {
      const response = await axios.post("/auth/login", credentials);
      const { token, user } = response.data;
      commit("setToken", token);
      commit("setUser", user);
      return user;
    },
    async register({ commit }, userData) {
      const response = await axios.post("/auth/register", userData);
      const { token, user } = response.data;
      commit("setToken", token);
      commit("setUser", user);
      return user;
    },
    logout({ commit }) {
      commit("clearAuth");
    },
    async bindWallet({ commit }, walletAddress) {
      const response = await axios.post("/wallet/bind", {
        wallet_address: walletAddress,
      });
      // 更新钱包信息
      if (response.data && response.data.wallet) {
        commit("setWalletInfo", response.data.wallet);
      }
      return response.data;
    },
    async getWalletInfo({ commit }) {
      const response = await axios.get("/wallet/info");
      commit("setWalletInfo", response.data);
      return response.data;
    },
    async getWalletBalance() {
      const response = await axios.get("/wallet/balance");
      return response.data;
    },
    async depositBalance({ commit, state }, amount) {
      const response = await axios.post("/wallet/deposit", { amount });
      // 更新钱包信息
      if (response.data && response.data.balance !== undefined) {
        const walletInfo = {
          ...state.walletInfo,
          balance: response.data.balance,
        };
        commit("setWalletInfo", walletInfo);
      }
      return response.data;
    },
    async mintNFT(contractAddress) {
      const response = await axios.post("/transaction/mint-nft", {
        nft_contract_address: contractAddress,
      });
      return response.data;
    },
    async getTransactionHistory({ commit }) {
      const response = await axios.get("/transaction/history");
      commit("setTransactions", response.data);
      return response.data;
    },
  },
  getters: {
    isAuthenticated: (state) => !!state.token,
    currentUser: (state) => state.user,
    walletAddress: (state) => state.walletInfo?.wallet_address || null,
    hasWallet: (state) => !!state.walletInfo?.wallet_address,
  },
});
